package com.br.modelo.dao;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class DaoResource {      
        EntityManagerFactory factory;
        EntityManager manager;
        
        public DaoResource(){
        	factory = Persistence.createEntityManagerFactory("insg");
        	manager = factory.createEntityManager();
        }
        
        public Date convertStringToDate(String data, String formato) throws ParseException {
                Date date = null;  
        try {  
            DateFormat formatter = new SimpleDateFormat(formato);  
            date = (Date)formatter.parse(data);  
        } catch (ParseException e) {              
            throw e;  
        }
        
        return date;
        }
        
        public <T> Object buscarPorId(Class<T> nomeClasse, Object Id) {

//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();

        	manager.getTransaction().begin();
        	Object obj = manager.find(nomeClasse, Id);
        	manager.getTransaction().commit();

        	factory.close();
        	return obj;
        }
                
        public List select(String query, String arg1, Object arg2) {
//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();
        	
        	manager.getTransaction().begin();
        	Query obj = manager.createQuery(query);
        	obj.setParameter(arg1, arg2);

        	manager.getTransaction().commit();      

        	factory.close();

        	List lista = obj.getResultList();

        	return lista;
        }
        
        public List select(String query) {
//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();

        	manager.getTransaction().begin();
        	Query obj = manager.createQuery(query);

        	manager.getTransaction().commit();      

        	factory.close();

        	List lista = obj.getResultList();

        	return lista;
        }
        
        public List select(String query, String arg1, Object arg2, String arg3, Object arg4) {
//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();

        	manager.getTransaction().begin();
        	Query obj = manager.createQuery(query);
        	obj.setParameter(arg1, arg2);
        	obj.setParameter(arg3, arg4);

        	manager.getTransaction().commit();      

        	factory.close();

        	List lista = obj.getResultList();

        	return lista;
        }
        
        public void save(Object obj) {
//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();

        	manager.getTransaction().begin();

        	manager.merge(obj);

        	manager.getTransaction().commit();

        	factory.close();
        } 

        public List saveAndReturnPrimaryKey(Object obj, String query) {
//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();

        	manager.getTransaction().begin();

        	manager.persist(obj);   
        	Query lista = manager.createQuery(query);

        	manager.getTransaction().commit();              
        	factory.close();                

        	List object = new ArrayList();
        	object = lista.getResultList();         

        	return object;
        }       

        public <T> void delete(Class<T> nomeClasse, Object Id) {
//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();

        	Object obj = buscarPorId(nomeClasse, Id);

        	manager.getTransaction().begin();
        	manager.remove(obj);
        	manager.getTransaction().commit();

        	factory.close();
        }       

        @SuppressWarnings("unchecked")
        public List listar(String query){
//        	factory = Persistence.createEntityManagerFactory("insg");
//        	manager = factory.createEntityManager();

        	manager.getTransaction().begin();
        	Query lista = manager.createQuery(query);
        	manager.getTransaction().commit();
        	factory.close();

        	List list = lista.getResultList();

        	return list;
        }
        
        public void endConnection() {
                factory.close();
        }
}